﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EAppointment.Models;
using System.Collections.Specialized;
using System.Globalization;

namespace EAppointment.Controllers
{
    public class CreditCardController : Controller
    {
        NameValueCollection HttpParams = System.Web.HttpContext.Current.Request.Params;

        UserLoginDetector userLoginDetector = new UserLoginDetector();

        public JsonpResult Index()
        {
            return Errors.ReturnError(ErrorTypes.NOT_FOUND);
        }

        [HttpPost]
        public JsonpResult Apply()
        {
            if (!userLoginDetector.Detect(ControllerContext))
                return Errors.ReturnError(ErrorTypes.NOT_LOGIN);

            long ID = userLoginDetector.UserID;

            int TypeID;
            int.TryParse(HttpParams["type"], out TypeID);
            string Location = HttpParams["location"];
            if (string.IsNullOrWhiteSpace(Location))
            {
                return Errors.ReturnError(ErrorTypes.PARAM_ERROR);
            }

            string DateStr = HttpParams["date"];
            DateTime date;
            DateTimeFormatInfo format = new DateTimeFormatInfo();
            format.FullDateTimePattern = "yyyy-MM-dd";
            try
            {
                date = DateTime.Parse(DateStr, format);
            }
            catch
            {
                return Errors.ReturnError(ErrorTypes.PARAM_ERROR);
            }

            int Period = 0;
            int.TryParse(HttpParams["period"], out Period);

            if (Period == 0)
                date = new DateTime(date.Year, date.Month, date.Day, 8, 0, 0);
            else if (Period == 1)
                date = new DateTime(date.Year, date.Month, date.Day, 14, 0, 0);

            DataSets.CreditCardTableAdapters.TB_CreditCardTableAdapter adapter = new DataSets.CreditCardTableAdapters.TB_CreditCardTableAdapter();
            int? Result = 0;
            adapter.Appoint_CreditCard(ID, TypeID, Location, date, ref Result);

            if (Result == 0)
            {
                return Succs.ReturnSuccess(SuccsTypes.APPOINT_SUCCESSED);
            }
            else
            {
                return Errors.ReturnError(ErrorTypes.APPOINT_ERROR);
            }
        }

        
    }
}
